package com.xindaoapp.frameworkfragment;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.alibaba.fastjson.JSON;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.squareup.picasso.Picasso;
import com.xindaoapp.framework.R;
import com.xindaoapp.framework.activity.CatListActivity;
import com.xindaoapp.framework.api.NewApiImpl;
import com.xindaoapp.framework.bean.GoodsOne;
import com.xindaoapp.framework.bean.GoodsOne.Cat;
import com.xindaoapp.framework.utils.IRequest;

/**
 * ============================================================
 * 
 * 版权 : 新道APP (c) 2014
 * 
 * 作者 : 贺奕凯
 * 
 * 版本 : 1.0
 * 
 * 创建日期 : 2014-6-24 下午8:38:45
 * 
 * 描述 : 分类
 * 
 * 修订历史 ：
 * 
 * ============================================================
 **/
public class ClassifyFragment extends Fragment {
	public Context ct;
	public SlidingMenu sm;
	@ViewInject(R.id.goods_one_list)
	private ListView one;
	@ViewInject(R.id.shop_second)
	private GridView second;
	@ViewInject(R.id.noresult)
	private RelativeLayout noresult;
	
	private NewApiImpl impl;

	public ClassifyFragment() {
		super();
	}
	
	public ClassifyFragment(Context ct) {
		this.ct = ct;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		impl=NewApiImpl.getInstance();
		getData();
		initClick();
		super.onActivityCreated(savedInstanceState);
	}
	private void initClick() {
		second.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				try {
					Intent intent=new Intent(ct, CatListActivity.class);
					intent.putExtra("catID", cats2.get(position).cat_id);
					intent.putExtra("catName", cats2.get(position).cat_name);
					startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment2, null);
		ViewUtils.inject(this, view);
		return view;
	}
	private GoodsOneAdapter oneAdapter;
	private static List<Cat> cats;
	private void getData() {
		impl.getCategoryOne(new IRequest<String>() {
			@Override
			public void request(String t) {
				if(t.equals("error")){
					
				}else{
					try {
						GoodsOne parse = JSON.parseObject(t, GoodsOne.class);
						if (parse.getStatus().equals("1")) {
							cats = parse.getData();
							cats.get(0).isChecked=true;
							initSecond(cats.get(0).cat_id);
							if (oneAdapter == null) {
								oneAdapter = new GoodsOneAdapter();
								one.setAdapter(oneAdapter);
							} else {
								oneAdapter.notifyDataSetChanged();
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
		one.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				try {
					oneAdapter.setSelectedPosition(position);
					oneAdapter.notifyDataSetChanged();
					initSecond(cats.get(position).cat_id);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	private List<Cat> cats2;
	private GoodSecondAdapter secondAdapter;
	private void initSecond(String cat_id) {
		impl.getCategoryTwo(cat_id, new IRequest<String>() {
			@Override
			public void request(String t) {
				System.out.println(t);
				if(t.equals("error")){
					
				}else{
					second.setVisibility(View.VISIBLE);
					noresult.setVisibility(View.GONE);
					try {
						GoodsOne parse = JSON.parseObject(t, GoodsOne.class);
						if (parse.getStatus().equals("1")) {
							cats2 = parse.getData();
							if(cats2!=null &&cats.size()!=0){
								secondAdapter = new GoodSecondAdapter(cats2);
								second.setAdapter(secondAdapter);
							}else{
								second.setVisibility(View.GONE);
								noresult.setVisibility(View.VISIBLE);
							}
						} 
						if(parse.getStatus().equals("0")&&cats2.size()==0){
							second.setVisibility(View.VISIBLE);
							Toast.makeText(ct, parse.getMsg(), Toast.LENGTH_SHORT).show();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
	}
	private class GoodsOneAdapter extends BaseAdapter {
		int selectedPosition = 0;

		@Override
		public int getCount() {
			return cats.size();
		}

		@Override
		public Object getItem(int position) {
			return cats.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
		public void setSelectedPosition(int position) {
			selectedPosition = position;
		}
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			final ViewHolderOne holder;
			if (convertView == null) {
				holder = new ViewHolderOne();
				convertView = View.inflate(ct, R.layout.goods_one_item, null);
				holder.imageView1 = (ImageView) convertView.findViewById(R.id.imageView1);
				holder.check = (CheckedTextView) convertView.findViewById(R.id.goods_one_check);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolderOne) convertView.getTag();
			}
			if (selectedPosition == position) {
				holder.check.setTextColor(new Color().RED);
				holder.check.setTextSize(18);
				holder.imageView1.setVisibility(View.GONE);
				convertView.setBackgroundResource(R.drawable.leimu_left_3);
			} else { 
				holder.check.setTextColor(new Color().GRAY);
				holder.check.setTextSize(16);
				holder.imageView1.setVisibility(View.GONE);
				convertView.setBackgroundResource(R.drawable.leinu_left_bg);
			}
			holder.check.setText(cats.get(position).cat_name);
			return convertView;
		}
	}
	class ViewHolderOne {
		CheckedTextView check;
		ImageView imageView1;
	}
	/**
	 * 右边种类列表
	 */
	private class GoodSecondAdapter extends BaseAdapter {
		private List<Cat> cats2;
		public GoodSecondAdapter(List<Cat> cats2) {
			super();
			this.cats2 = cats2;
		}
		@Override
		public int getCount() {
			return cats2.size();
		}
		@Override
		public Object getItem(int position) {
			return cats2.get(position);
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolderSecond holder;
			if(convertView==null){
				convertView=View.inflate(ct,R.layout.goods_second_item, null);
				holder=new ViewHolderSecond();
				holder.pic=(ImageView) convertView.findViewById(R.id.goods_second_pic);
				holder.name=(TextView) convertView.findViewById(R.id.goods_second_name);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolderSecond) convertView.getTag();
			}
			holder.name.setText(cats2.get(position).cat_name);
			if(cats2.get(position).img_url!=null&&!cats2.get(position).img_url.equals("")){
				Picasso.with(ct).load(cats2.get(position).img_url)
				.into(holder.pic);
			}else{
				holder.pic.setImageResource(R.drawable.leimu_right_item_bg);
			}
			
			return convertView;
		}
	}

	class ViewHolderSecond {
		ImageView pic;
		TextView name;
	}
	@Override
	public void setMenuVisibility(boolean menuVisible) {
		super.setMenuVisibility(menuVisible);
		if (this.getView() != null)
			this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
	}
}
